// pages/course/course.js
const app=getApp();
Page({
  /**
   * 页面的初始数据
   */
  data:{
    setInterval:false,
    pageNum:1,
    pageSize:4,
    url: '',
    courseList: [],
    imageList:[]
   },
   gojingjiangCourse:function(e){
     const id=e.currentTarget.dataset.id;
     console.log(id);
     wx.navigateTo({
      //  url: './pages/course/jingjiangCourse?id='+id
      url:'./jingjiangCourse/jingjiangCourse?id='+id
     });
   },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
     // 获取全局的url赋值给当前页面的URL变量
     this.setData({
      url:app.globalData.URL
       })
    // 动态调用后端接口 
    let that = this
    console.log(app.globalData.URL) 
    wx.request({
      url: that.data.url+'course/findAll?pageNum='+that.data.pageNum+'&pageSize='+that.data.pageSize,
      success(resp){
        console.log(resp.data.data.list)
        let slicedList = resp.data.data.list.slice(0, 4);
        let tabularCharts = slicedList.map(item => item);
        that.setData({
          courseList:resp.data.data.list,
          imageList:tabularCharts
        })
        }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      pageNum: 1,
      pageSize: 4,
      courseList: [],
      imageList: []
  });
  let that = this;
  // 重新发起请求
  wx.request({
      url: that.data.url + 'course/findAll?pageNum=' + that.data.pageNum + '&pageSize=' + that.data.pageSize,
      success(resp) {
          console.log(resp.data.data.list)
          let slicedList = resp.data.data.list.slice(0, 4);
          let tabularCharts = slicedList.map(item => item);
          that.setData({
              courseList: resp.data.data.list,
              imageList: tabularCharts
          });
      }
  })
  },
  
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    let that=this;
    that.setData({
      pageSize:that.data.pageSize+2,
      setInterval:true
    });
      setTimeout(function () {
        that.setData({
          setInterval:false
        });
          }, 2000); 
    wx.request({
      url: that.data.url+'course/findAll?pageNum='+that.data.pageNum+'&pageSize='+that.data.pageSize,
      success(resp){
        that.setData({
          courseList:resp.data.data.list
        })
        }
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  goSearch() {
    wx.navigateTo({
      url: '/pages/course/search/search',
    })
  },
  goOnline(){
    wx.navigateTo({
      url: '/pages/course/onlineCourse/onlineCourse',
    })
  },
  goShizi(){
    wx.navigateTo({
        url: '/pages/course/shizi/shizi',
      })
  },
  gozxgao(){
    wx.navigateTo({
      url: '/pages/course/shizi/zhangjing/zhangjing',
    })

  }
})